<?php

class LessonDiscussionController extends \BaseController {

    /**
     * 返回某一课程的讨论列表.
     *
     * @param $lessonId 待查询的课程id
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
	public function index($lessonId)
	{
        $rep = Lesson_Discussion::getLessonDiscussions($lessonId);

        return Response::json($rep);
	}

    /**
     * 新建当前课程的讨论.
     *
     * @param $lessonId 当前选择的课程id
     * @return \Illuminate\Http\JsonResponse
     *
     * 返回的JSON格式：
     * {
     *   "id": "新增课程讨论的id"
     * }
     */
	public function store($lessonId)
	{
        $postData['title'] = Purifier::clean( Input::get('title') );
        $postData['content'] = Purifier::clean( Input::get('content') );

        $validator = $this->getDiscussionValidator($postData);

        if( $validator->passes() ){
            $postData['creater_id'] = Auth::user()['id'];
            $postData['lesson_id'] = $lessonId;

            $newDiscussion = Lesson_Discussion::create($postData);

            return Response::json([
                'id'=>$newDiscussion['id']
            ]);
        } else {
            return Response::json([
                'error'=> $this->changeValidatorMessageToString( $validator->messages() )
            ], 403);
        }
	}

    /**
     * 返回讨论信息所使用的校验器.
     *
     * @param $postData
     * @return \Illuminate\Validation\Validator
     */
    protected function getDiscussionValidator($postData)
    {
        $verifyRules= [
            'title'=>'required',
            'content'=>'required'
        ];

        return Validator::make($postData, $verifyRules);
    }

    /**
     * 返回某一课程的某一个讨论.
     *
     * @param $lessonId 待查询课程的id
     * @param $discussionId 待查询的课程讨论id
     * @return \Illuminate\Http\JsonResponse
     *
     * 返回的JSON格式：
     * {
     *   "id": "讨论id",
     *   "title": "讨论的标题",
     *   "content": "讨论的内容",
     *   "updated_at": "讨论的更新时间",
     *   "created_at": "讨论的创建时间",
     *   "creater_id": "创建讨论的人的id",
     *   "creater": {
     *      "id": "创建者的用户id",
     *      "username": "创建者的用户名",
     *      "head_image": "创建者的头像",
     *  },
     *  "reply": [
     *      //回复
     *      {
     *          "content": "内容",
     *          "created_at": "创建时间",
     *          "updated_at": "更新时间",
     *          "creater_id": "创建id",
     *          "creater": {
     *               //创建者信息，同上，省略
     *          }
     *      },
     *      //注意是个数组
     *  ]
     * }
     */
    public function show($lessonId, $discussionId)
    {
        $targetDiscussion = Lesson_Discussion::findDiscussionOnLessonOrFail($lessonId, $discussionId);

        $resp = $targetDiscussion->toArray();
        $resp['creater'] = $targetDiscussion['creater']->toArray();
        $resp['replies'] = Lesson_Discussion_Reply::getDiscussionReplies($discussionId);

        return Response::json($resp);
    }

    /**
     * 更改讨论.
     *
     * @param $lessonId
     * @param $discussionId
     * @return \Illuminate\Http\Response
     */
    public function update($lessonId, $discussionId)
    {
        $targetDiscussion = Lesson_Discussion::findDiscussionOnLessonOrFail($lessonId, $discussionId);

        $keys = ['title', 'content'];
        $putData = [];
        foreach($keys as $currentKey){
            if( Input::get( $currentKey ) ){
                $putData[ $currentKey ] = Purifier::clean( Input::get($currentKey) );
            }
        }

        $targetDiscussion->update($putData);

        return Response::make('', 200);
    }

    /**
     * 删除讨论
     *
     * @param $lessonId
     * @param $discussionId
     * @return \Illuminate\Http\Response
     * @throws Exception
     */
    public function destroy($lessonId, $discussionId)
    {
        $targetDiscussion = Lesson_Discussion::findDiscussionOnLessonOrFail($lessonId, $discussionId);
        $targetDiscussion->delete();

        return Response::make('', 200);
    }

}
